import { SendMessageProps } from "../types";
import { lookup } from "mime-types";
import fs from "fs/promises";

export async function convertMessage(messages: SendMessageProps[]) {
  const convertedMessages = [];
  for (const message of messages) {
    let messageContent: string | any[] = "";
    if (message.imagePath) {
      const mimeType = lookup(message.imagePath);
      const imageBuffer = await fs.readFile(message.imagePath);
      const base64Image = imageBuffer.toString("base64");
      messageContent = [
        { type: "text", text: message.content },
        {
          type: "image_url",
          image_url: { url: `data:${mimeType};base64,${base64Image}` },
        },
      ];
    } else {
      messageContent = message.content;
    }
    const { imagePath, ...withoutImagePath } = message;
    convertedMessages.push({ ...withoutImagePath, content: messageContent });
    console.log({ ...withoutImagePath, content: messageContent }, "信息");
  }
  return convertedMessages;
}
